'use strict';

initBanner();
initRecord();
noLogin();
var urlHead = 'http://kmb.leehq.com/pc/';
//初始化顶部banner
function initBanner() {
    $.houseSlider({
        $ctrList: $('.slider .ctl li'),
        $cont: $('.slider .cont'),
        isLoop: true,
        animateType: $.houseSlider.HOUSE_SLIDER_FADEIN,
        autoPlay: 5000
    });
}
// 最新成交
function initRecord() {

    var addCardList = new Vue({
        el: '#main-content',
        data: {
            'isLogin': false,
            'cardList': [],
            'addCardList': [],
            'addCardColor': [],
            'contentList': {},
            'urlHeadCommon': 'http://kmb.leehq.com/pc/',
            'cardListType': 'N',
            'remindInfo': '添加成功！',
            'cardListInfo': [],
            'cardListTypeArr': [],
            'pageNum': 1,
            'pageIndex': 1,
            'remindInfo1': '',
            'loading': true,
            'isWriteActive ': false,
            'iscardList': false,
            'isPageOne': true
        },
        components: {},
        watch: {},
        created: function created() {
            if (sessionStorage.getItem('token')) {
                this.isLogin = true;
                this.isWriteActive = false;
            }
            this.cardListFun();
            this.contentListFun('N', 1, 0);
        },
        mounted: function mounted() {
            $('.page-size').eq(0).addClass('current');
        },

        methods: {
            cardListFun: function cardListFun(cb) {
                var _this = this;

                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'p/cardlist' + '?token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                    //beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');}
                });
                ajax.done(function (data) {
                    _this.cardList = data.data.cardList;
                    _this.cardListTypeArr = [];
                    for (var i = 0; i < _this.cardList.length; i++) {
                        _this.cardListInfo.push({ listInfo: _this.cardList[i].cardNum ? _this.cardList[i].cardNum : 0 });

                        _this.cardListTypeArr.push(_this.cardList[i].id);
                    }
                    _this.iscardList = true;
                    cb ? cb() : cb = 0;
                });
            },
            contentListFun: function contentListFun(type, pageNo, qType) {
                var _this2 = this;

                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';
                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'p/currencylist?NA=' + type + '&pageNo=' + pageNo + '&pageSize=20&qtype=' + qType + '&token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                });
                ajax.done(function (data) {
                    _this2.cardListType = type;
                    _this2.showAddListAjax(_this2.cardListType);
                    _this2.contentListData = data.data;
                    _this2.contentList = data.data.dpage;
                    _this2.pageNum = Math.ceil(data.data.dpage.count / 20);

                    _this2.loading = false;
                    $('.loading').hide();
                    $('#main-content').show();
                });
            },
            changeList: function changeList(qType) {
                this.contentListFun(this.cardListType, this.pageIndex, qType);
            },
            addListAjax: function addListAjax(addType, str) {
                var _this3 = this;

                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';
                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'c/mem/createFav?curNoList=' + addType + '&token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                });
                ajax.done(function () {
                    _this3.showAddListAjax(_this3.cardListType);
                    _this3.remindInfo = str;
                    _this3.remindInfo1 = '';
                    $('.remind-info').show();
                    var t = setTimeout(function () {
                        $('.remind-info').hide();
                    }, 1000);
                });
            },
            showAddListAjax: function showAddListAjax(type) {
                var _this4 = this;

                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'p/favCurrencylist?NA=' + type + '&token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                });
                ajax.done(function (data) {
                    if (data.data) {
                        _this4.addCardList = data.data.favList;
                        _this4.addCardList.forEach(function (val, index) {
                            _this4.addCardColor.push({});
                            Vue.set(_this4.addCardColor[index], 'color', val.color);
                        });
                    } else {
                        _this4.addCardList = [];
                    }
                });
            },
            sureReportAjax: function sureReportAjax(params) {
                var _this5 = this;

                this.loading = true;
                $('.loading').show();
                var token = sessionStorage.getItem('token') || '';
                var telNo = sessionStorage.getItem('phone') || '';

                var ajax = $.ajax({
                    url: this.urlHeadCommon + 'c/mem/createPool?cardList=' + params + '&token=' + token + '&telNo=' + telNo,
                    type: 'GET',
                    header: { token: sessionStorage.getItem('token') || '', telNo: sessionStorage.getItem('phone') || '' }
                });
                ajax.done(function (data) {
                    if (data.status == 200) {
                        _this5.cardListFun(function () {
                            _this5.contentListFun(_this5.cardListType, _this5.pageIndex, 0);
                        });
                    } else {
                        _this5.loading = false;
                        $('.loading').hide();
                        _this5.remindInfo = '加载失败';
                        $('.remind-info').show();
                        var t = setTimeout(function () {
                            $('.remind-info').hide();
                        }, 1000);
                    }
                });
            },
            topUpMember: function topUpMember() {
                if (!this.isLogin) {
                    this.isWriteActive = true;
                    $('.login-page').show();
                }
            },
            changeN: function changeN() {
                this.contentListFun('N', this.pageIndex, 0);
            },
            changeA: function changeA() {
                this.contentListFun('A', this.pageIndex, 0);
            },
            addList: function addList(index) {
                if (this.isLogin) {
                    var addLength = this.addCardList.length;
                    var curNos = '';
                    if (addLength < 3) {
                        if (addLength === 0) {
                            curNos = this.contentList.list[index].curyNo;
                        } else {
                            var curNosArr = [];
                            for (var i = 0; i < addLength; i++) {
                                if (this.contentList.list[index].curyNo === this.addCardList[i].curyNo) {
                                    this.remindInfo = '添加失败，已添加' + this.contentList.list[index].curyNo + '币';
                                    $('.remind-info').show();
                                    var t = setTimeout(function () {
                                        $('.remind-info').hide();
                                    }, 1000);
                                    return;
                                } else {
                                    curNosArr.push(this.addCardList[i].curyNo);
                                }
                            }
                            curNosArr.push(this.contentList.list[index].curyNo);
                            curNos = curNosArr.join(',');
                        }
                        this.addListAjax(curNos, '添加成功');
                    } else {
                        this.remindInfo = '只能收藏3个哟';
                        this.remindInfo1 = '（必须先删除一个）';

                        $('.remind-info').show();
                        var t = setTimeout(function () {
                            $('.remind-info').hide();
                        }, 1000);
                    }
                } else {
                    if (!this.isLogin) {
                        $('.login-page').show();
                    }
                }
            },
            removeList: function removeList(index) {
                var curNos = '';
                if (this.addCardList.length !== 1) {
                    if (this.addCardList.length <= 3) {
                        this.addCardList.splice(index, 1);
                        if (this.addCardList.length == 1) {
                            curNos = this.addCardList[0].curyNo;
                            //this.cardListHeight.height = '102px';
                        } else {
                            curNos = this.addCardList[0].curyNo + ',' + this.addCardList[1].curyNo;
                            //this.cardListHeight.height = '204px';
                        }
                    } else {
                        curNos = this.addCardList[0].curyNo;
                    }
                } else {
                    this.addCardList = [];
                    curNos = '';
                }
                this.addListAjax(curNos, '删除成功');
            },
            sureReport: function sureReport() {
                for (var i = 0; i < this.cardListInfo.length; i++) {
                    if (this.cardListInfo[i].listInfo) {
                        this.cardListTypeArr[i] = this.cardListTypeArr[i] + '-' + this.cardListInfo[i].listInfo;
                    } else {
                        this.cardListTypeArr[i] = this.cardListTypeArr[i] + '-0';
                    }
                }
                console.log(this.cardListTypeArr);
                var str = this.cardListTypeArr.join(',');
                this.sureReportAjax(str);
            },

            nextPage: function nextPage(index) {
                this.pageIndex = index;
                $('.page-size').removeClass('current');
                $('.page-size').eq(index - 1).addClass('current');
                this.contentListFun(this.cardListType, index, 0);
            },
            clickCallback: function clickCallback(pageNum) {
                console.log(pageNum);
                this.pageIndex = pageNum;
                this.contentListFun(this.cardListType, pageNum, 0);
                if (this.pageIndex !== 1) {
                    this.isPageOne = false;
                } else {
                    this.isPageOne = true;
                }
            }
        }

    });

    // let cardsListInfo = new Vue({
    //     el:'#cards-list-info',
    //     data:{
    //
    //     }
    // });


    $('.operators-write').on('click', function () {
        $('.operators-list').css('height', 'auto');
        $(this).removeClass('active');
        $('.operators-sure').addClass('active');
        //$('.operators-list').animate({'height':200 + 'px'},500);
    });
    $('.operators-sure').on('click', function () {
        $(this).removeClass('active');
        $('.operators-write').addClass('active');
        $('.operators-list').animate({ 'height': 0 + 'px' }, 500);
    });

    $('.pull-more').on('click', function () {
        if ($(this).hasClass('show-list')) {
            var listLength = $('.card-list-info-content .card-list-info').length;
            if (listLength === 2) {
                $('.card-list-info-content').animate({ 'height': '254px' }, 500, function () {
                    $(this).css('height', 'auto');
                });
            } else if (listLength === 3) {
                $('.card-list-info-content').animate({ 'height': '372px' }, 500, function () {
                    $(this).css('height', 'auto');
                });
            }
            $('.pull-more').css({ 'transform': 'rotate(180deg)' }).addClass('hide-list').removeClass('show-list');
        } else {
            $('.card-list-info-content').animate({ 'height': '127px' }, 500);
            $('.pull-more').css({ 'transform': 'rotate(0)' }).addClass('show-list').removeClass('hide-list');
        }
    });

    $('.register-button').on('click', function () {
        $('.login-page').hide();
        $('.register-page').show();
    });
    $('.register-page').on('click', function (e) {
        if (!$('.register-content').is(e.target) && $('.register-content').has(e.target).length === 0) {
            $('.register-page').hide();
        }
    });
    $('.close-icon').on('click', function () {
        $('.fixed-page').hide();
    });
    $('.close-remind').on('click', function () {
        $('.remind-info').hide();
    });
    $('.login-button').on('click', function () {
        $('.fixed-page').hide();
        $('.login-page').show();
    });

    var registerData = {
        memberName: '',
        gender: 1,
        telNo: 0,
        verCodeSure: 0,
        verCode: 0,
        password: '',
        invitationCode: ''

    };

    var forgetData = {
        telNo: 0,
        verCodeSure: 0,
        verCode: 0,
        password: '',
        repassword: ''
    };
    //验证昵称
    $('.name-input input').blur(function () {
        var name = $(this).val();
        if (!name) {
            $('.name-style').show();
        } else {
            registerData.memberName = name;
            $('.name-style').hide();
        }
    });
    //验证性别
    $('#man').on('click', function () {
        $('#man').attr('data-gender', '1');
        $('#woman').attr('data-gender', '0');
        registerData.gender = 1;
    });

    $('#woman').on('click', function () {
        $('#man').attr('data-gender', '0');
        $('#woman').attr('data-gender', '1');
        registerData.gender = 2;
    });

    //验证电话号码
    $('.phone-input input').blur(function () {
        var phone = $(this).val();

        if (!/^1[34578]\d{9}$/.test(phone)) {
            $('.phone-style').show();
        } else {
            $('.phone-style').hide();
            registerData.telNo = phone;
        }
    });
    //得到邀请码
    $('.receive-code input').blur(function () {
        registerData.invitationCode = $(this).val();
    });
    //获取验证码
    $('.send-phone').on('click', function () {
        var phone = $('.phone-input input').val();
        if (!phone) {
            $('.phone-style').show();
        } else {
            $('.phone-style').hide();
            codeAjax(phone, 1001).done(function () {
                $('.send-phone').removeClass('active');
                $('.register-send-code').addClass('active');
                $('.register-send-code').html('重新发送60s');

                var time = 60;
                var timer = setInterval(function () {
                    time--;
                    $('.register-send-code').html('重新发送' + time + 's');
                    if (time === 0) {
                        $('.register-send-code').removeClass('active');
                        $('.send-phone').addClass('active');
                        window.clearTimeout(timer);
                    }
                }, 1000);
            });
        }
    });
    //验证 验证码
    $('.code-input').blur(function () {
        var code = $(this).val();
        if (!code) {
            $('.code-style').show();
        } else {
            registerData.verCode = code;
            $('.code-style').hide();
        }
    });
    //验证密码
    $('.phone-rePassword input').blur(function () {
        var rePassword = $('input[name="pw2"]').val();
        var password = $('input[name="pw1"]').val();
        console.log(rePassword, password);
        if (rePassword !== password) {
            $('.mis-password').show();
        } else {
            $('.mis-password').hide();
            registerData.password = password;
        }
    });
    //forget 发送验证码
    $('.send-phone1').on('click', function () {
        var phone = $('.phone-input1 input').val();
        if (!phone) {
            $('.phone-style').show();
        } else {
            $('.phone-style').hide();
            codeAjax(phone, 1001).done(function () {
                $('.send-phone1').removeClass('active');
                $('.forget-send-code').addClass('active');
                $('.forget-send-code').html('重新发送60s');

                var time = 60;
                var timer = setInterval(function () {
                    time--;
                    $('.forget-send-code').html('重新发送' + time + 's');
                    if (time === 0) {
                        $('.forget-send-code').removeClass('active');
                        $('.send-phone1').addClass('active');
                        window.clearTimeout(timer);
                    }
                }, 1000);
            });
        }
    });

    //forgot 验证电话号码
    $('.phone-input1 input').blur(function () {
        var phone = $(this).val();

        if (!/^1[34578]\d{9}$/.test(phone)) {
            $('.phone-style').show();
        } else {
            $('.phone-style').hide();
            forgetData.telNo = phone;
        }
    });

    $('.code-input1').blur(function () {
        var code = $(this).val();
        if (!code) {
            $('.code-style').show();
        } else {
            $('.code-style').hide();
            forgetData.verCode = code;
        }
    });

    //forget 验证密码
    $('.phone-rePassword1 input').blur(function () {
        var rePassword = $('input[name="pw6"]').val();
        forgetData.repassword = rePassword;
        var password = $('input[name="pw5"]').val();
        console.log(rePassword, password);
        if (password) {
            forgetData.password = password;
            if (rePassword !== password) {
                $('.mis-password2').show();
            } else {
                $('.mis-password1').hide();
                $('.mis-password2').hide();
                forgetData.password = password;
                forgetData.repassword = password;
            }
        } else {
            $('.mis-password1').show();
        }
    });

    //忘记密码
    $('.forget-but').on('click', function () {
        if (forgetData.telNo) {
            if (forgetData.verCode) {
                recodeAjax(forgetData.telNo, forgetData.verCode).done(function (data) {

                    if (data.status == 200) {
                        $('.code-style').hide();
                        if (forgetData.password) {
                            if (forgetData.password === forgetData.repassword) {
                                forgetAjax(forgetData).done(function (data) {
                                    $('.fixed-page').hide();
                                    $('.success-remind').html(data.message);
                                    $('.register-success p').html('修改成功');
                                    $('.register-success').show();
                                });
                            } else {
                                $('.mis-password2').show();
                            }
                        } else {
                            $('.mis-password1').show();
                        }
                    } else {
                        $('.code-style').show();
                    }
                });
            } else {
                $('.code-style').show();
            }
        } else {
            $('.phone-style').show();
        }
    });

    //点击注册
    $('.register-but').on('click', function () {
        if (registerData.memberName) {
            if (registerData.telNo) {
                debugger;
                if (registerData.verCode) {
                    recodeAjax(registerData.telNo, registerData.verCode).done(function (data) {
                        if (data.status == 200) {
                            if (registerData.password) {
                                registerAjax(registerData).done(function (data) {
                                    $('.fixed-page').hide();
                                    $('.success-remind').html(data.message);
                                    $('.register-success').show();
                                });
                            } else {
                                $('.password-style').show();
                            }
                        } else {
                            $('.code-style').show();
                        }
                    });
                } else {
                    $('.code-style').show();
                }
            } else {
                $('.phone-style').show();
            }
        } else {
            $('.name-style').show();
        }
    });

    //忘记密码
    $('.forget-password').on('click', function () {
        $('.fixed-page').hide();
        $('.forget-page').show();
    });
    //登录
    $('.login-button-home').on('click', function () {
        var phone1 = $('input[name="tel1"]').val();
        var password1 = $('input[name="pw3"]').val();
        var phone2 = $('input[name="tel2"]').val();
        var password2 = $('input[name="pw4"]').val();
        if (phone1 && password1 || phone2 && password2) {
            var phone = 0;
            var password = 0;
            if (phone1 && password1) {
                phone = phone1;
                password = password1;
            } else {
                phone = phone2;
                password = password2;
            }
            loginAjax(phone, password).done(function (data) {
                if (data.status === 200) {
                    sessionStorage.setItem('token', data.data.token);
                    sessionStorage.setItem('phone', phone);
                    $('.login-style').hide();
                    $('.fixed-page').hide();
                    noLogin();
                    // window.location.reload();
                    location.reload();
                    //$('body').append("<script type='text/javascript'>window.location.reload(true);</script>");
                } else {
                    $('.login-style').html(data.message);
                    $('.login-style').show();
                }
            }).fail(function (data) {
                $('.login-style').html(data.message);
                $('.login-style').show();
            });
        } else {
            $('.login-style').show();
        }
    });

    $('.card-type').on('click', function () {
        $(this).addClass('active').siblings().removeClass('active');
    });
    $('.tab-btn').on('click', function () {
        $(this).addClass('active');
        $(this).siblings('.tab-btn').removeClass('active');
        window.location.href = $(this).attr('data-href');
    });

    $('.tab-btn4').on('click', function () {
        $('.pay-page').show();
    });
    $('.pay-internet,.pay-munu ').on('click', function () {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
}
function loginAjax(phone, password) {
    return $.ajax({
        url: urlHead + 'p/mem/loginPsw?telNo=' + phone + '&password=' + password,
        type: 'POST'
    });
}

function registerAjax(data) {
    return $.ajax({
        url: urlHead + 'p/mem/register?memberName=' + data.memberName + '&telNo=' + data.telNo + '&password=' + data.password + '&verCode=' + data.verCode + '&gender=' + data.gender + '&invitationCode=' + data.invitationCode,
        type: 'POST'
    });
}

function forgetAjax(data) {
    return $.ajax({
        url: urlHead + 'p/mem/resetPsw?telNo=' + data.telNo + '&code=' + data.verCode + '&psw=' + data.password,
        type: 'POST'
    });
}
function codeAjax(phone, type) {
    var ajax = $.ajax({
        url: urlHead + 'p/mem/sendMobileCode?telNo=' + phone + '&smsType=' + type,
        type: 'GET'
    });
    return ajax;
}
function recodeAjax(phone, code) {
    var ajax = $.ajax({
        url: urlHead + 'p/mem/veryMobileCode?telNo=' + phone + '&veryCode=' + code,
        type: 'GET'
    });
    return ajax;
}
function noLogin() {
    if (sessionStorage.getItem('token')) {
        $('.login-button-go').html('欢迎，来到矿脉宝！').removeClass('login-button');
        $('.login-box,.register-remind').hide();
    }
}